
# =============================================================
# File: geography_rep.R
# Purpose: Plots kernel densities for representativeness of studies
# Date: 19 July 2019
# Data: ./geography_rep.dta

# Techincal disclaimer:
# All analyses in R version 3.5.0 (2018-04-23) using RStudio Version 1.1.453 on Windows 10, 64 bit
#	Machine: Intel(R) Core(TM) i7-4650U CPU @ 1.70Ghz 2.30Ghz with 8GB RAM
# =============================================================


# Load library
library(foreign)
library(ggplot2)
library(cowplot)


# Load data
data <- read.dta("./geography_rep.dta")

# Color settings: colorblind-friendly palette
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")


# =============================================================
# Plot begins here
# =============================================================

data <- data[data$year==2013,]



# Density for logged GDP per capita
tgdp <- t.test(log(data$gdppc)~data$study)

p1 <- ggplot() +
      geom_density(data=data, aes(x=log(gdppc), fill=as.factor(study)), na.rm=TRUE, lwd=1, alpha=0.6)+
      scale_fill_manual(values=c(cols[1],cols[3]), labels=c(" Excluded  ", " In sample")) +
      theme(legend.position="bottom", legend.title=element_blank()) +
      labs(x="log GDP per capita", y="Density") +
      annotate("text", x=9.8, y=0.33, label=paste("t-statistic:", round(tgdp$statistic["t"],3)), hjust=0)+
      annotate("text", x=9.8, y=0.31, label=paste("p-value:", round(tgdp$p.value,3)), hjust=0)
p1
ggsave(p1,filename="./p1.png",width=6, height=5)		  



# Density for Polity IV
tpol <- t.test(data$polity~data$study)

p2 <- ggplot() +
      geom_density(data=data, aes(x=polity, fill=as.factor(study)), na.rm=TRUE, lwd=1, alpha=0.6)+
      scale_fill_manual(values=c(cols[1],cols[3]), labels=c(" Excluded  ", " In sample")) +
      theme(legend.position="bottom", legend.title=element_blank()) +
      labs(x="Polity IV democracy score", y="Density") +
      annotate("text", x=-9, y=0.1, label=paste("t-statistic:", round(tpol$statistic["t"],3)), hjust=0)+
      annotate("text", x=-9, y=0.093, label=paste("p-value:", round(tpol$p.value,3)), hjust=0)
p2
ggsave(p2,filename="./p2.png",width=6, height=5)		  


# Density for logged population density
tpop <- t.test(log(data$popdens)~data$study)


p3 <- ggplot() +
  geom_density(data=data, aes(x=log(popdens), fill=as.factor(study)), na.rm=TRUE, lwd=1, alpha=0.6)+
  scale_fill_manual(values=c(cols[1],cols[3]), labels=c(" Excluded  ", " In sample")) +
  theme(legend.position="bottom", legend.title=element_blank()) +
  labs(x="log population density", y="Density") +
  annotate("text", x=7, y=0.29, label=paste("t-statistic:", round(tpop$statistic["t"],3)), hjust=0)+
  annotate("text", x=7, y=0.27, label=paste("p-value:", round(tpop$p.value,3)), hjust=0)
p3
ggsave(p3,filename="./p3.png",width=6, height=5)		  




       
#ggsave(p,filename="./study_map.png",width=10, height=4)		  
	
# =============================================================

